<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sample-paint</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.2.39/vue.global.min.js"></script>
    <style>
        canvas{
            border:1px solid gray;
        }
    </style>
</head>

<body>
    <div id="app">
        <input type="text" name="" v-model="connectURL" id=""> <button @click="connect">连接</button>
        <div>
            <canvas id="canvas" width="800" height="600"></canvas>
        </div>
        <hr>
        接收用户id：<input type="text" name="" v-model="recvid" id=""><br>
        <textarea name="" id="" cols="30" rows="10" v-model="txt"></textarea><br>
        <!-- <button @click="sendmsg">发送SEND</button> -->
        <button @click="ack">收到ACK</button>
        登录用户id：
        {{ uid }}
    </div>
    <script>
        const { createApp } = Vue
        var wsclient = null
        var onmessage=function(ev){
            let msg = JSON.parse(ev.data)
            if(msg.Type=="notify_id"){
                app.uid=msg.Msg
                // app.list.splice(0,0,"连接时间 "+(new Date()).toString())
            }
            if(msg.Type=="receive"){
                var msgstr = msg.Msg.Msg
                var xylist = msgstr.split(';')
                var xy = xylist[0].split(',')
                var xy2 = xylist[1].split(',')
                var pt = new Path2D()
                pt.moveTo(xy[0]*1,xy[1]*1)
                pt.lineTo(xy2[0]*1,xy2[1]*1)
                ctx.strokeStyle="#ff0000" //红色网络绘制
                ctx.stroke(pt)

                console.log(ctx,xy,xy2)
                // app.list.splice(0,0,msg.SendId+" ----> "+msg.Msg.Msg)
            }
            if(msg.Type=="notify_ack"){
                // app.list.splice(0,0,msg.SendId+" 表示收到你的信息")
            }
            console.log(msg)
        }
        var onclose=function(){
            alert('退出了')
            app.uid=''
            // setTimeout(() => {
            //     //3s重连一次
            //     wsclient = new WebSocket("ws://127.0.0.1:9999/ws")
            //     wsclient.onmessage=onmessage
            //     wsclient.onclose=onclose
            // }, 3000);
        }
        
        // var context = document.querySelector("#canvas").getContext("2d")
       

        var app = createApp({
            data() {
                return {
                    connectURL:"ws://127.0.0.1:9999/ws",
                    list:[
                    ],
                    uid:"登录态id",
                    recvid:"",
                    txt:""
                }
            },
            mounted(){
                var canvas = document.querySelector("canvas")
                console.log(canvas)
                var ctx = canvas.getContext("2d")
                window.ctx = ctx
                var lastX = 0
                var lastY = 0
                var down = false
                canvas.onmousedown=function(ev){
                    
                    if(!wsclient){
                        alert('请连接后操作')
                        return
                    }

                    lastX = ev.offsetX
                    lastY = ev.offsetY
                    down=true
                    console.log(lastX,lastY,ev)
                }
                var next = true
                canvas.onmousemove=function(ev){
                    if(down){
                        if(next){
                            next=false
                            setTimeout(() => {
                                next=true
                                // ev.offsetX-lastX
                                // ev.offsetY-lastY
                                app.send_paint(lastX,lastY,ev.offsetX,ev.offsetY)
                                
                                var pt = new Path2D()
                                pt.moveTo(lastX,lastY)
                                pt.lineTo(ev.offsetX,ev.offsetY)
                                ctx.strokeStyle="#000000" //黑色本地绘制
                                ctx.stroke(pt)

                                lastX = ev.offsetX
                                lastY = ev.offsetY

                            }, 10);
                        }
                    }
                }
                canvas.onmouseup=function(ev){
                    ev.offsetX
                    ev.offsetY
                    down=false
                }
            },
            methods:{
                connect(){
                    if(wsclient!=null){
                        wsclient.close()
                    }
                    wsclient =  new WebSocket(this.connectURL)
                    wsclient.onmessage=onmessage
                    wsclient.onclose=onclose
                },
                ack(){
                    wsclient.send(JSON.stringify({
                        Id:"abc",
                        SendId:this.uid,
                        Type:"notify_ack",
                        Msg:{
                            RecvId:this.recvid,
                            Received:1
                        }
                    }))
                },
                sendmsg(){
                    wsclient.send(JSON.stringify({
                        Id:"abc",
                        SendId:this.uid,
                        Type:"send",
                        Msg:{
                            RecvId:this.recvid,
                            Msg:this.txt
                        }
                    }))
                },
                send_paint(x,y,x1,y1){
                    wsclient.send(JSON.stringify({
                        Id:"abc",
                        SendId:this.uid,
                        Type:"send",
                        Msg:{
                            RecvId:this.recvid,
                            Msg:x+","+y+";"+x1+","+y1
                        }
                    }))
                }
            }
        }).mount('#app')
    </script>
</body>

</html>